<?php

header("Content-Type: text/html", false);
header("Content-Type: text/html; charset=UTF-8", false);
header("Charset: charset=UTF-8", false);
header("Charset: UTF-8", false);
header("Charset: text/html; charset=UTF-8", false);

//setcookie("s_status", "0", time()+$conf['timeout']+31536000, "/");

include "kbh.cfg.php";

//           0   1    2     3      4         5     6
// online : date¦ip¦level¦score¦hexaname¦appTitle¦name

$defLang = "english";
$langs=array("english", "français", "magyar", "română", "türkçe", "简体中文");

$defPage = "toplist";
$pages=array("toplist", "songlist");

$var = explode("?",$_REQUEST['var'],2);
if($_REQUEST['lang']=="" && $var[0]!="") {
  $_REQUEST['lang']=$var[0];
}
if($_REQUEST['name']=="" && $var[1]!="") {
  $_REQUEST['name']=$var[1];
}

if(get_magic_quotes_gpc()) {
  $_REQUEST['name']=stripslashes($_REQUEST['name']);
  $_REQUEST['lang']=stripslashes($_REQUEST['lang']);
}

if(!isset($_REQUEST['lang']) || !in_array(($lang=strtolower($_REQUEST['lang'])), $langs)) {
  $lang = $defLang;
}

if(!isset($_REQUEST['page']) || !in_array(($page=strtolower($_REQUEST['page'])), $pages)) {
  $page = $defPage;
}

switch($lang) {
  case 'français':
    $tx=array();
    $tx['toplist']="Toplist";
    $tx['songlist']="Liste&nbsp;de&nbsp;chansons";
    $tx['EmptyToplist']="EmptyToplist";
    $tx['Name']="Nom";
    $tx['Score']="Score";
    $tx['Speed']="Vitese";
    $tx['Song']="Chanson";
    $tx['Version']="Version";
    $tx['Anonymous']="Anonymous";
    $tx['DateTime']="Date";
    $tx['Download']="Téléchargement";
    $tx['EmptySonglist']="Liste de chansons est vide!";
    $tx['Filesize']="Filesize";
  break;
  
  case 'magyar':
    $tx=array();
    $tx['toplist']="Toplista";
    $tx['songlist']="Dal&nbsp;Lista";
    $tx['EmptyToplist']="A toplista üres!";
    $tx['Name']="Név";
    $tx['Score']="Pontszám";
    $tx['Speed']="Sebesség";
    $tx['Song']="Dal";
    $tx['Version']="Verzió";
    $tx['Anonymous']="Névtelen";
    $tx['DateTime']="Időpont";
    $tx['Download']="Letölt";
    $tx['EmptySonglist']="A dallista üres!";
    $tx['Filesize']="Fájlméret";
  break;
  
  case 'română':
    $tx=array();
    $tx['toplist']="Toplist";
    $tx['songlist']="Lista&nbsp;de&nbsp;cantece";
    $tx['EmptyToplist']="Toplist-ul este gol!";
    $tx['Name']="Nume";
    $tx['Score']="Scor";
    $tx['Speed']="Viteza";
    $tx['Song']="Cantec";
    $tx['Version']="Versiune";
    $tx['Anonymous']="Anonim";
    $tx['DateTime']="Data";
    $tx['Download']="Descarcare";
    $tx['EmptySonglist']="Lista de cantece-ul este gol!";
    $tx['Filesize']="Filesize";
  break;
  
  case 'türkçe':
    $tx=array();
    $tx['toplist']="Toplist";
    $tx['songlist']="Şarkı Listesi ";
    $tx['EmptyToplist']="Toplist Boş!";
    $tx['Name']="İsim";
    $tx['Score']="Skor";
    $tx['Speed']="Hız";
    $tx['Song']="Şarkı";
    $tx['Version']="Versiyon";
    $tx['Anonymous']="İsimsiz";
    $tx['DateTime']="Tarih";
    $tx['Download']="İndir";
    $tx['EmptySonglist']="Şarkı Listesi Boş!";
    $tx['Filesize']="Filesize";
  break;
  
  case '简体中文':
    $tx=array();
    $tx['toplist']="排行榜";
    $tx['songlist']="Song&nbsp;List";
    $tx['EmptyToplist']="空的排行榜!";
    $tx['Name']="昵称";
    $tx['Score']="分数";
    $tx['Speed']="速度";
    $tx['Song']="曲子";
    $tx['Version']="版本";
    $tx['Anonymous']="匿名玩家";
    $tx['DateTime']="Date";
    $tx['Download']="Download";
    $tx['EmptySonglist']="The Song List is empty!";
    $tx['Filesize']="Filesize";
  break;
  
  default: // case 'english':
    $tx=array();
    $tx['toplist']="Toplist";
    $tx['songlist']="Song&nbsp;List";
    $tx['EmptyToplist']="The Toplist is empty!";
    $tx['Name']="Name";
    $tx['Score']="Score";
    $tx['Speed']="Speed";
    $tx['Song']="Song";
    $tx['Version']="Version";
    $tx['Anonymous']="Anonymous";
    $tx['DateTime']="Date";
    $tx['Download']="Download";
    $tx['EmptySonglist']="The Song List is empty!";
    $tx['Filesize']="Filesize";
  break;
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="hu" xml:lang="hu">
<head>
  <title><?php echo $title." - ".$tx[$page]; ?></title>
  
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <meta http-equiv="charset" content="text/html; charset=utf-8" />
  
  <link rel="shortcut icon" href="favicon.ico" />
  <link rel="icon" href="favicon.ico" />
  
  <style type="text/css">
    body {
      font-family: Arial, Verdana, Tahoma;
      font-size: 10pt;
      background: #12293E;
      color: #ffffff;
    }
    td {
      text-align: center;
    }
    .title {
      font-weight:bold;
      font-size:18pt;
    }
    .subtitle {
      font-weight:bold;
      font-size:14pt;
    }
    .intop {
      font-weight:bold;
    }
    .samename {
      color: #FCFF1F;
      font-weight:bold;
      font-style:italic;
    }
		a {
			color: #FCFF1F;
		}
		a:hover {
			color: #67711A;
		}
  </style>

</head>
<body>
<?php
$hexd = array('0'=>0, '1'=>1, '2'=>2, '3'=>3, '4'=>4, '5'=>5, '6'=>6, '7'=>7, '8'=>8, '9'=>9, 'A'=>10, 'B'=>11, 'C'=>12, 'D'=>13, 'E'=>14, 'F'=>15);
/*
function dehex($str) {
  global $hexd;
  $txt="";
  $max = strlen($str);
  for($i=0;$i<$max;$i+=4) {
    $txt.="&#".str_pad($hexd[$str{$i}]*4096+$hexd[$str{$i+1}]*256+$hexd[$str{$i+2}]*16+$hexd[$str{$i+3}], 3, "0", STR_PAD_LEFT).";";
  }
  return $txt;
}*/

function dehex($str) {
  global $hexd;
  $txt="";
  $max = strlen($str);
  for($i=0;$i<$max;$i+=4) {
    $txt.=utf8chr(str_pad($hexd[$str{$i}]*4096+$hexd[$str{$i+1}]*256+$hexd[$str{$i+2}]*16+$hexd[$str{$i+3}], 3, "0", STR_PAD_LEFT));
  }
  return $txt;
}

function utf8chr($data) {
  if ($data > 127) {
    $i = 5;
    while (($i--) > 0) {
      if ($data != ($a = $data % ($p = pow(64, $i)))) {
        $ret = chr(base_convert(str_pad(str_repeat(1, $i + 1), 8, "0"), 2, 10) + (($data - $a) / $p));
        for ($i; $i > 0; $i--) {
          $ret .= chr(128 + ((($data % pow(64, $i)) - ($data % ($p = pow(64, $i - 1)))) / $p));
        }
        break;
      }
    }
    return $ret;
  }
  else {
    return chr($data);
  }
}

echo "<h1 align='center'><a href='http://keyboardhero.co.cc' title='www.keyboardhero.co.cc'><img src='KeyboardHero.png' border='0' alt='".$title."' /></a></h1>";
echo "<center style='font-size:12pt'>
[".($page=='toplist'?$tx[$page]:"<a href='KeyboardHero.php?page=toplist&amp;name=".$_REQUEST['name']."&amp;lang=".$lang."'>".$tx['toplist']."</a>")."]&nbsp;| 
[".($page=='songlist'?$tx[$page]:"<a href='KeyboardHero.php?page=songlist&amp;name=".$_REQUEST['name']."&amp;lang=".$lang."'>".$tx['songlist']."</a>")."]&nbsp;| 
[<a href='kbh.php?req=upddownload'>".$tx['Download']."&nbsp;KeyboardHero.jar</a>]&nbsp;| 
[<a href='KeyboardHero.libs.zip'>".$tx['Download']."&nbsp;KeyboardHero.libs.zip</a>]
</center><br />";

echo "<center>";
$first = true;
foreach($langs as $l) {
  if($first) $first=false; else echo "&nbsp;| \n";
  if($lang == $l) {
    echo "[".strtoupper($l{0}).substr($l, 1)."]";
  } else {
    echo "[<a href='KeyboardHero.php?page=".$page."&amp;name=".$_REQUEST['name']."&amp;lang=".$l."'>".strtoupper($l{0}).substr($l, 1)."</a>]";
  }
}
echo "</center><br />";

echo "<br /><h2 align='center' class='title'>".$tx[$page]."</h2>";

if($page == "toplist") {
  $fd = @fopen($toplistfile, "r");
  if($fd) {
    echo "<br />\n\n<table align='center' width='80%'>\n";
    echo "<tr class='subtitle'><td></td><td>".$tx['Name']."</td><td>".$tx['Score']."</td><td>".$tx['Song']."</td><td>".$tx['Speed']."</td><td>".$tx['DateTime']."</td><td>".$tx['Version']."</td></tr>\n";
    echo "<tr colspan='7'><td>&nbsp;</td></tr>\n";
    $i=0;
    
    $reqname = $_REQUEST['name'];
    $reqname2 = stripslashes($reqname);
    
    while(!feof($fd)) {
      $l=fgets($fd);
      if(strlen($l)>=6) {
        if($i == $maxinverytop) {
          echo "<tr><td colspan='7'><hr /></td></tr>";
        }
        
        $l=explode("¦",$l,8);
        $l2 = explode("¦", dehex($l[4]));
        $song = $l2[0];
        $name = $l2[1];
        echo "<tr".(( $name === $reqname || $name === $reqname2) && $name !== "" ? " class='samename'" : ($i<$maxintop?" class='intop'":"")).($i<$maxinverytop?" style='font-size:12pt;'":"")."><td>".(++$i).".</td><td>".($name==""?$tx['Anonymous']:htmlspecialchars($name, ENT_QUOTES))."</td><td>".$l[3]."</td><td>".$song."</td><td>".$l[2]."</td><td>".$l[0]."</td><td>".htmlspecialchars($l[5], ENT_QUOTES)."</td></tr>\n";
      }
    }
    fclose($fd);
    echo "</table>\n\n";
    if($i==0) {
      echo "<center class='subtitle'><i>".$tx['EmptyToplist']."</i></center>";
    }
  }
  else {
    echo "<br /><center class='subtitle'><i>".$tx['EmptyToplist']."</i></center>";
  }
} else if($page == "songlist") {
  $directory = "midifiles";

  function formatbytes($val, $digits = 3, $mode = "SI", $bB = "B"){ //$mode == "SI"|"IEC", $bB == "b"|"B"
         $si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y");
         $iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi");
         switch(strtoupper($mode)) {
             case "SI" : $factor = 1000; $symbols = $si; break;
             case "IEC" : $factor = 1024; $symbols = $iec; break;
             default : $factor = 1000; $symbols = $si; break;
         }
         switch($bB) {
             case "b" : $val *= 8; break;
             default : $bB = "B"; break;
         }
         for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++)
             $val /= $factor;
         $p = strpos($val, ".");
         if($p !== false && $p > $digits) $val = round($val);
         elseif($p !== false) $val = round($val, $digits-$p);
         return round($val, $digits) . " " . $symbols[$i] . $bB;
     }

  $d = dir($directory);
  $array=array();
  $ord=intval($_REQUEST['ord']);
  if($ord<0 || $ord>2) $ord=0;
  switch($ord) {
    case 1:
      while (false !== ($entry = $d->read())) {
        $array[$entry]=filesize($directory."/".$entry);
      }
    break;
    
    case 2:
      while (false !== ($entry = $d->read())) {
        $array[$entry]=filemtime($directory."/".$entry);
      }
    break;
    
    default:
      while (false !== ($entry = $d->read())) {
        $array[$entry]=$entry;
      }
    break;
  }
  $d->close();

  if($_REQUEST['ord2']=='1') {
    $ord2=1;
    arsort($array);
  } else {
    $ord2=0;
    asort($array);
  }
  
  echo "<br />\n\n<table align='center' width='50%'>";
  echo "<tr class='subtitle'>
<td><a".($ord==0?" font-style:italic;":"")." href='KeyboardHero.php?page=".$page."&amp;name=".$_REQUEST['name']."&amp;lang=".$lang."&amp;ord=0&amp;ord2=".($ord==0&&$ord2!=1?'1':'0')."'>".$tx['Song']."</a></td>
<td><a".($ord==1?" font-style:italic;":"")." href='KeyboardHero.php?page=".$page."&amp;name=".$_REQUEST['name']."&amp;lang=".$lang."&amp;ord=1&amp;ord2=".($ord==1&&$ord2!=1?'1':'0')."'>".$tx['Filesize']."</a></td>
<td><a".($ord==2?" font-style:italic;":"")." href='KeyboardHero.php?page=".$page."&amp;name=".$_REQUEST['name']."&amp;lang=".$lang."&amp;ord=2&amp;ord2=".($ord==2&&$ord2!=1?'1':'0')."'>".$tx['DateTime']."</a></td>
</tr>\n";
  echo "<tr colspan='3'><td>&nbsp;</td></tr>\n";
  $i=0;
  foreach($array as $entry=>$ordering) {
    if(eregi("\.(mid|midi)$", $entry) && !(eregi("\.php.?$", $entry) ||  $entry{0}=="." || is_dir($directory."/".$entry) /*miert nem konyvtar a .. ??*/ || $entry == "." || $entry == "..")) {
      $i++;
      echo "\n<tr><td><a href='".$directory."/".$entry."'>".$entry."</a></td><td>".formatbytes(filesize($directory."/".$entry), 3, "IEC", "B")."</td><td>".date("Y-m-d H:i:s", filemtime($directory."/".$entry))."</td></tr>";
    }
  }
  echo "\n</table>";
  
  if($i==0) {
      echo "<center class='subtitle'><i>".$tx['EmptySonglist']."</i></center>";
    }
}
?>
</body>
</html>